package com.example.produces.controller;

import com.example.produces.entity.Pay;
import com.example.produces.service.PayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/Pay")
@CrossOrigin//允许跨域请求
@Slf4j
@Transactional
public class PayController {
    @Autowired
    private PayService service;
    private Map<String,Object> map=new HashMap<>();
    @GetMapping("/findAll")
    public Map<String,Object>findAll(){
        try {
            List<Pay> pays=service.findAll();

            map.put("records",pays);
            map.put("state",true);
            map.put("code",200);
            map.put("msg","success");
        }catch (Exception e){
            e.printStackTrace();
            map.put("state",false);
            map.put("code",500);
            map.put("msg","fail");
        }
        return map;
    }
    @GetMapping("/findOne")
    public Map<String,Object> findOne(String designation){
        try {
            Pay pay=service.findOne(designation);
            map.put("records",pay);
            map.put("state",true);
            map.put("code",200);
            map.put("msg","success");
        }catch (Exception e){
            e.printStackTrace();
            map.put("state",false);
            map.put("code",500);
            map.put("msg","fail");
        }
        return map;
    }
    @PostMapping("/add")
    public Map<String,Object> add(@RequestBody Pay pay){
        try {
            service.add(pay);
            map.put("state",true);
            map.put("code",200);
            map.put("msg","success");
        }catch (Exception e){
            e.printStackTrace();
            map.put("state",false);
            map.put("code",500);
            map.put("msg","fail");
        }
        return map;
    }
    @PostMapping ("/upData")
    public Map<String,Object> upData(@RequestBody Pay pay){
        try {
            service.upDate(pay);
            map.put("state",true);
            map.put("code",200);
            map.put("msg","success");
        }catch (Exception e){
            e.printStackTrace();
            map.put("state",false);
            map.put("code",500);
            map.put("msg","fail");
        }
        return map;
    }
    @GetMapping("/delete")
    public Map<String,Object> delete(String retainage){
        try {
            service.delete(retainage);
            map.put("state",true);
            map.put("code",200);
            map.put("msg","success");
        }catch (Exception e){
            e.printStackTrace();
            map.put("state",false);
            map.put("code",500);
            map.put("msg","fail");
        }
        return map;
    }
}
